R screenshot
New York Times, Jan 2009
We can do lot of stuffs in R. Starting from statestical analysis to plotting graphs and figures, Writing technical documentation to making a website and lot more. Lets explore.
https://www.facebook.com/notes/facebook-engineering/visualizing-friendships/469716398919
http://www.revolutionanalytics.com/companies-using-r
Sidney Harris - New York Times
New York Times, July 2011
According to recent editorials, the reproducibility crisis is still on-going
Nature, May 2016
R can be done/executed using command line, or a graphical user interface (GUI). On this course, we use the RStudio GUI. Lets download required files and install.
To launch RStudio, find the RStudio icon and click
RStudio screenshot
To make the best of the R language, you’ll need a strong understanding of the basic data types and data structures and how to operate on them.
Data structures are very important to understand because these are the objects you will manipulate on a day-to-day basis in R. Dealing with object conversions is one of the most common sources of frustration for beginners.
Everything in R is an object.
R has 6 basic data types. (In addition to the five listed below, there is also raw which will not be discussed in this session.)
Elements of these data types may be combined to form data structures, such as atomic vectors. When we call a vector atomic, we mean that the vector only holds data of a single data type. Below are examples of atomic character vectors, numeric vectors, integer vectors, etc.
"a", "swc"2, 15.52L (the L tells R to store this as an integer)TRUE, FALSE1+4i (complex numbers with real and imaginary parts)R provides many functions to examine features of vectors and other objects, for example
class() - what kind of object is it (high-level)?typeof() - what is the object’s data type (low-level)?length() - how long is it? What about two dimensional objects?attributes() - does it have any metadata?## [1] "character"
## NULL
## [1] 1 2 3 4 5 6 7 8 9 10
## [1] "integer"
## [1] 10
## [1] 1 2 3 4 5 6 7 8 9 10
## [1] "double"
R has many data structures. These include
A vector is the most common and basic data structure in R and is pretty much the workhorse of R. Technically, vectors can be one of two types:
although the term “vector” most commonly refers to the atomic types not to lists.
A vector is a collection of elements that are most commonly of mode character, logical, integer or numeric.
You can create an empty vector with vector(). (By default the mode is logical. You can be more explicit as shown in the examples below.) It is more common to use direct constructors such as character(), numeric(), etc.
## logical(0)
## [1] "" "" "" "" ""
## [1] "" "" "" "" ""
## [1] 0 0 0 0 0
## [1] FALSE FALSE FALSE FALSE FALSE
You can also create vectors by directly specifying their content. R will then guess the appropriate mode of storage for the vector. For instance:
will create a vector x of mode numeric. These are the most common kind, and are treated as double precision real numbers. If you wanted to explicitly create integers, you need to add an L to each element (or coerce to the integer type using as.integer()).
Using TRUE and FALSE will create a vector of mode logical:
While using quoted text will create a vector of mode character:
The functions typeof(), length(), class() and str() provide useful information about your vectors and R objects in general.
## [1] "character"
## [1] 3
## [1] "character"
## chr [1:3] "Sarah" "Tracy" "Jon"
The function c() (for combine) can also be used to add elements to a vector.
## [1] "Sarah" "Tracy" "Jon" "Annette"
## [1] "Greg" "Sarah" "Tracy" "Jon" "Annette"
You can create vectors as a sequence of numbers.
## [1] 1 2 3 4 5 6 7 8 9 10
## [1] 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4
## [16] 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9
## [31] 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.0 5.1 5.2 5.3 5.4
## [46] 5.5 5.6 5.7 5.8 5.9 6.0 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9
## [61] 7.0 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 8.0 8.1 8.2 8.3 8.4
## [76] 8.5 8.6 8.7 8.8 8.9 9.0 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9
## [91] 10.0
R supports missing data in vectors. They are represented as NA (Not Available) and can be used for all the vector types covered in this lesson:
The function is.na() indicates the elements of the vectors that represent missing data, and the function anyNA() returns TRUE if the vector contains any missing values:
## [1] FALSE TRUE FALSE FALSE TRUE
## [1] FALSE FALSE FALSE FALSE FALSE
## [1] TRUE
## [1] FALSE
Inf is infinity. You can have either positive or negative infinity.
## [1] Inf
NaN means Not a Number. It’s an undefined value.
## [1] NaN
R will create a resulting vector with a mode that can most easily accommodate all the elements it contains. This conversion between modes of storage is called “coercion”. When R converts the mode of storage based on its content, it is referred to as “implicit coercion”. For instance, can you guess what the following do (without running them first)?
You can also control how vectors are coerced explicitly using the as.<class_name>() functions:
## [1] 1
## [1] "1" "2"
Finding Commonalities
Do you see a property that’s common to all these vectors above?
Solution
All vectors are one-dimensional and each element is of the same type.
Objects can have attributes. Attributes are part of the object. These include:
You can also glean other attribute-like information such as length (works on vectors and lists) or number of characters (for character strings).
## [1] 10
## [1] 18
In R matrices are an extension of the numeric or character vectors. They are not a separate type of object but simply an atomic vector with dimensions; the number of rows and columns. As with atomic vectors, the elements of a matrix must be of the same data type.
## [,1] [,2]
## [1,] NA NA
## [2,] NA NA
## [1] 2 2
You can check that matrices are vectors with a class attribute of matrix by using class() and typeof().
## [1] "matrix" "array"
## [1] "integer"
While class() shows that m is a matrix, typeof() shows that fundamentally the matrix is an integer vector.
Data types of matrix elements
Consider the following matrix:
Given that
typeof(FOURS[1])returns"double", what would you expecttypeof(FOURS)to return? How do you know this is the case even without running this code?Hint Can matrices be composed of elements of different data types?
Solution
We know thattypeof(FOURS)will also return"double"since matrices are made of elements of the same data type. Note that you could do something likeas.character(FOURS)if you needed the elements ofFOURSas characters.
Matrices in R are filled column-wise.
Other ways to construct a matrix
This takes a vector and transforms it into a matrix with 2 rows and 5 columns.
Another way is to bind columns or rows using rbind() and cbind() (“row bind” and “column bind”, respectively).
## x y
## [1,] 1 10
## [2,] 2 11
## [3,] 3 12
## [,1] [,2] [,3]
## x 1 2 3
## y 10 11 12
You can also use the byrow argument to specify how the matrix is filled. From R’s own documentation:
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 11 12 13
Elements of a matrix can be referenced by specifying the index along each dimension (e.g. “row” and “column”) in single square brackets.
## [1] 13
In R lists act as containers. Unlike atomic vectors, the contents of a list are not restricted to a single mode and can encompass any mixture of data types. Lists are sometimes called generic vectors, because the elements of a list can by of any type of R object, even lists containing further lists. This property makes them fundamentally different from atomic vectors.
A list is a special type of vector. Each element can be a different type.
Create lists using list() or coerce other objects using as.list(). An empty list of the required length can be created using vector()
## [[1]]
## [1] 1
##
## [[2]]
## [1] "a"
##
## [[3]]
## [1] TRUE
##
## [[4]]
## [1] 1+4i
## [1] 5
The content of elements of a list can be retrieved by using double square brackets.
## NULL
Vectors can be coerced to lists as follows:
## [1] 10
Examining Lists
- What is the class of
x[1]?- What is the class of
x[[1]]?Solution
1.```r class(x[1]) ``` ``` ## [1] "list" ```
## [1] "integer"
Elements of a list can be named (i.e. lists can have the names attribute)
## $a
## [1] "Karthik Ram"
##
## $b
## [1] 1 2 3 4 5 6 7 8 9 10
##
## $data
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
## [1] "a" "b" "data"
Examining Named Lists
- What is the length of this object?
- What is its structure?
Solution
1.```r length(xlist) ``` ``` ## [1] 3 ```
{: .solution} {: .challenge} Lists can be extremely useful inside functions. Because the functions in R are able to return only a single object, you can “staple” together lots of different kinds of results into a single object that a function can return.## List of 3 ## $ a : chr "Karthik Ram" ## $ b : int [1:10] 1 2 3 4 5 6 7 8 9 10 ## $ data:'data.frame': 6 obs. of 5 variables: ## ..$ Sepal.Length: num [1:6] 5.1 4.9 4.7 4.6 5 5.4 ## ..$ Sepal.Width : num [1:6] 3.5 3 3.2 3.1 3.6 3.9 ## ..$ Petal.Length: num [1:6] 1.4 1.4 1.3 1.5 1.4 1.7 ## ..$ Petal.Width : num [1:6] 0.2 0.2 0.2 0.2 0.2 0.4 ## ..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1
A list does not print to the console like a vector. Instead, each element of the list starts on a new line.
Elements are indexed by double brackets. Single brackets will still return another list. If the elements of a list are named, they can be referenced by the $ notation (i.e. xlist$data).
A data frame is a very important data type in R. It’s pretty much the de facto data structure for most tabular data and what we use for statistics.
A data frame is a special type of list where every element of the list has same length (i.e. data frame is a “rectangular” list).
Data frames can have additional attributes such as rownames(), which can be useful for annotating data, like subject_id or sample_id. But most of the time they are not used.
Some additional information on data frames:
read.csv() and read.table(), i.e. when importing the data into R.data.frame() function.nrow(dat) and ncol(dat), respectively.To create data frames by hand:
## id x y
## 1 a 1 11
## 2 b 2 12
## 3 c 3 13
## 4 d 4 14
## 5 e 5 15
## 6 f 6 16
## 7 g 7 17
## 8 h 8 18
## 9 i 9 19
## 10 j 10 20
Useful Data Frame Functions
head()- shows first 6 rowstail()- shows last 6 rowsdim()- returns the dimensions of data frame (i.e. number of rows and number of columns)nrow()- number of rowsncol()- number of columnsstr()- structure of data frame - name, type and preview of data in each columnnames()orcolnames()- both show thenamesattribute for a data framesapply(dataframe, class)- shows the class of each column in the data frame {: .callout} See that it is actually a special list:
## [1] TRUE
## [1] "data.frame"
Because data frames are rectangular, elements of data frame can be referenced by specifying the row and the column index in single square brackets (similar to matrix).
## [1] 11
As data frames are also lists, it is possible to refer to columns (which are elements of such list) using the list notation, i.e. either double square brackets or a $.
## [1] 11 12 13 14 15 16 17 18 19 20
## [1] 11 12 13 14 15 16 17 18 19 20
The following table summarizes the one-dimensional and two-dimensional data structures in R in relation to diversity of data types they can contain.
| Dimensions | Homogenous | Heterogeneous |
|---|---|---|
| 1-D | atomic vector | list |
| 2-D | matrix | data frame |
Lists can contain elements that are themselves muti-dimensional (e.g. a lists can contain data frames or another type of objects). Lists can also contain elements of any length, therefore list do not necessarily have to be “rectangular”. However in order for the list to qualify as a data frame, the length of each element has to be the same. {: .callout} Column Types in Data Frames
Knowing that data frames are lists, can columns be of different type?
What type of structure do you expect to see when you explore the structure of the
irisdata frame? Hint: Usestr().Solution
The Sepal.Length, Sepal.Width, Petal.Length and Petal.Width columns are all numeric types, while Species is a Factor. Lists can have elements of different types. Since a Data Frame is just a special type of list, it can have columns of differing type (although, remember that type must be consistent within each column!).## 'data.frame': 150 obs. of 5 variables: ## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ... ## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ... ## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ... ## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ... ## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
typeof() and class() to know a object type and its class[row, column]read.table()read.csv(), read.delim()write.table()write.csv()Like many programs R has a concept of a working directory
It is the place where R will look for files to execute and where it will save files, by default
For this course we need to set the working directory to the location of the course scripts
In R Studio use the mouse and browse to the directory where you saved the Course Materials
Session → Set Working Directory → Choose Directory…
Before we even start the analysis, we need to be sure of where the data are located on our hard drive
## [1] "/home/sangram/Documents/personal-work/0_git/learn-R/website"
file.exists function does exactly what it says on the tin!
## [1] TRUE
file.choose()patients)In the latest RStudio, there is the option to import data directly from the File menu. File -> Import Dataset -> From Csv
sep="," or the function read.csv():## ID.Race.Sex.Smokes.Height.Weight.State.Pet.Grade.Age
## 1 AC/AH/001\tWhite\tMale\tNon-Smoker\t182.87\t76.57\tGeorgia\tDog\t2\t85
## 2 AC/AH/017\tWhite\tMale\tNon-Smoker\t179.12\t80.43\tMissouri\tDog\t2\t85
## 3 AC/AH/020\tBlack\tMale\tNon-Smoker\t169.15\t75.48\tPennsylvania\tNone\t2\t47
## 4 AC/AH/022\tWhite\tMale\tNon-Smoker\t175.66\t94.54\tFlorida\tCat\t1\t72
## 5 AC/AH/029\tWhite\tFemale\tNon-Smoker\t164.47\t71.78\tIowa\tNA\t2\t70
## 6 AC/AH/033\tNA\tFemale\tSmoker\t158.27\t69.9\tMaryland\tDog\t2\t66
## ID Race Sex Smokes Height Weight State Pet Grade
## 1 AC/AH/001 White Male Non-Smoker 182.87 76.57 Georgia Dog 2
## 2 AC/AH/017 White Male Non-Smoker 179.12 80.43 Missouri Dog 2
## 3 AC/AH/020 Black Male Non-Smoker 169.15 75.48 Pennsylvania None 2
## 4 AC/AH/022 White Male Non-Smoker 175.66 94.54 Florida Cat 1
## 5 AC/AH/029 White Female Non-Smoker 164.47 71.78 Iowa <NA> 2
## 6 AC/AH/033 <NA> Female Smoker 158.27 69.90 Maryland Dog 2
## 7 AC/AH/037 White Female Non-Smoker 161.69 68.85 Pennsylvania None 1
## 8 AC/AH/044 White Female Non-Smoker 165.84 70.44 North Carolina None 1
## 9 AC/AH/045 White Male Non-Smoker 181.32 76.90 Louisiana Dog 1
## 10 AC/AH/048 Hispanic Male Non-Smoker 167.37 79.06 North Carolina None 2
## Age
## 1 85
## 2 85
## 3 47
## 4 72
## 5 70
## 6 66
## 7 24
## 8 68
## 9 86
## 10 63
View() function to get a display of the data in RStudio:## [1] "data.frame"
## [1] 10
## [1] 100
## [1] 100 10
## [1] "ID" "Race" "Sex" "Smokes" "Height" "Weight" "State" "Pet"
## [9] "Grade" "Age"
## [1] "AC/AH/001" "AC/AH/017" "AC/AH/020" "AC/AH/022" "AC/AH/029" "AC/AH/033"
## [7] "AC/AH/037" "AC/AH/044" "AC/AH/045" "AC/AH/048" "AC/AH/049" "AC/AH/050"
## [13] "AC/AH/052" "AC/AH/053" "AC/AH/057" "AC/AH/061" "AC/AH/063" "AC/AH/076"
## [19] "AC/AH/077" "AC/AH/086" "AC/AH/089" "AC/AH/100" "AC/AH/104" "AC/AH/112"
## [25] "AC/AH/113" "AC/AH/114" "AC/AH/115" "AC/AH/127" "AC/AH/133" "AC/AH/150"
## [31] "AC/AH/154" "AC/AH/156" "AC/AH/159" "AC/AH/160" "AC/AH/164" "AC/AH/171"
## [37] "AC/AH/176" "AC/AH/180" "AC/AH/185" "AC/AH/186" "AC/AH/192" "AC/AH/198"
## [43] "AC/AH/207" "AC/AH/208" "AC/AH/210" "AC/AH/211" "AC/AH/213" "AC/AH/219"
## [49] "AC/AH/220" "AC/AH/221" "AC/AH/225" "AC/AH/233" "AC/AH/241" "AC/AH/244"
## [55] "AC/AH/248" "AC/AH/249" "AC/SG/002" "AC/SG/003" "AC/SG/008" "AC/SG/009"
## [61] "AC/SG/010" "AC/SG/015" "AC/SG/016" "AC/SG/046" "AC/SG/055" "AC/SG/056"
## [67] "AC/SG/064" "AC/SG/065" "AC/SG/067" "AC/SG/068" "AC/SG/072" "AC/SG/074"
## [73] "AC/SG/084" "AC/SG/095" "AC/SG/099" "AC/SG/101" "AC/SG/107" "AC/SG/116"
## [79] "AC/SG/121" "AC/SG/122" "AC/SG/123" "AC/SG/134" "AC/SG/139" "AC/SG/142"
## [85] "AC/SG/155" "AC/SG/165" "AC/SG/167" "AC/SG/172" "AC/SG/173" "AC/SG/179"
## [91] "AC/SG/181" "AC/SG/182" "AC/SG/191" "AC/SG/193" "AC/SG/194" "AC/SG/197"
## [97] "AC/SG/204" "AC/SG/216" "AC/SG/217" "AC/SG/234"
Like families, tidy datasets are all alike but every messy dataset is messy in its own way - (Hadley Wickham - RStudio chief scientist and author of dplyr, ggplot2 and others) You will make your life a lot easier if you keep your data tidy and organised. Before blaming R, consider if your data are in a suitable form for analysis. The more manual manipulation you have done on the data (highlighting, formulas, copy-and-pasting), the less happy R is going to be to read it. Here are some useful links on some common pitfalls and how to avoid them
NA values, which means the values are missing – a common occurrence in real data collectionNA is a special value that can be present in objects of any type (logical, character, numeric etc)NA is not the same as NULL:
NULL is an empty R object.NA is one missing value within an R object (like a data frame or a vector)NAs gracefully:## [1] 100
## [1] NA
NAs, and functions often have their own arguments (like na.rm) for handling them:
NA values. Always check the documentation## [1] 167.4969
## [1] 167.4969
# Create an index of results:
BMI <- (patients$Weight)/((patients$Height/100)^2)
upper.limit <- mean(BMI,na.rm = TRUE) + 2*sd(BMI,na.rm = TRUE)
upper.limit## [1] 30.9533
## [1] 22.9 25.1 26.4 30.6 26.5 27.9 26.3 25.6 23.4 28.2 28.2 NA 30.0 27.9 24.5
## [16] 22.0 25.6 31.5 23.8 NA 23.5 26.7 31.4 NA 24.6 NA 24.8 29.2 NA 24.1
## [31] 25.1 28.0 29.4 28.2 23.6 26.4 NA 25.0 27.7 27.0 25.6 26.7 24.5 26.1 23.1
## [46] 28.2 26.9 NA 25.4 25.9 NA 24.8 28.2 NA 30.4 26.8 26.0 25.2 26.9 31.7
## [61] 25.6 NA 26.7 27.8 28.4 NA 31.5 27.0 30.0 26.5 25.2 NA 26.7 25.8 NA
## [76] 27.6 29.1 26.6 26.6 26.9 27.6 26.4 27.8 NA 27.8 25.8 27.7 28.7 24.2 24.6
## [91] 28.3 24.8 27.8 21.4 28.0 26.0 26.2 26.4 27.7 NA
## ID Race Sex Smokes Height Weight State Pet Grade Age
## 1 AC/AH/001 White Male Non-Smoker 182.87 76.57 Georgia Dog 2 85
## 2 AC/AH/017 White Male Non-Smoker 179.12 80.43 Missouri Dog 2 85
## 3 AC/AH/020 Black Male Non-Smoker 169.15 75.48 Pennsylvania None 2 47
## 4 AC/AH/022 White Male Non-Smoker 175.66 94.54 Florida Cat 1 72
## 5 AC/AH/029 White Female Non-Smoker 164.47 71.78 Iowa <NA> 2 70
## 6 AC/AH/033 <NA> Female Smoker 158.27 69.90 Maryland Dog 2 66
## BMI
## 1 22.9
## 2 25.1
## 3 26.4
## 4 30.6
## 5 26.5
## 6 27.9
<- is doing an assignment. The value we are assigning to our new variable is the logical (TRUE or FALSE) vector given by testing each item in BMI against the upper.limit## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE NA
## [13] FALSE FALSE FALSE FALSE FALSE TRUE FALSE NA FALSE FALSE TRUE NA
## [25] FALSE NA FALSE FALSE NA FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [37] NA FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE NA
## [49] FALSE FALSE NA FALSE FALSE NA FALSE FALSE FALSE FALSE FALSE TRUE
## [61] FALSE NA FALSE FALSE FALSE NA TRUE FALSE FALSE FALSE FALSE NA
## [73] FALSE FALSE NA FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE NA
## [85] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [97] FALSE FALSE FALSE NA
We have seen that a logical vector can be used to subset a data frame
## ID Race Sex Smokes Height Weight State Pet Grade Age
## NA <NA> <NA> <NA> <NA> NA NA <NA> <NA> NA NA
## 18 AC/AH/076 White Male Non-Smoker 176.22 97.67 Louisiana Cat 2 26
## NA.1 <NA> <NA> <NA> <NA> NA NA <NA> <NA> NA NA
## 23 AC/AH/104 White Male Smoker 169.85 90.63 Kentucky None 1 87
## NA.2 <NA> <NA> <NA> <NA> NA NA <NA> <NA> NA NA
## NA.3 <NA> <NA> <NA> <NA> NA NA <NA> <NA> NA NA
## NA.4 <NA> <NA> <NA> <NA> NA NA <NA> <NA> NA NA
## NA.5 <NA> <NA> <NA> <NA> NA NA <NA> <NA> NA NA
## NA.6 <NA> <NA> <NA> <NA> NA NA <NA> <NA> NA NA
## NA.7 <NA> <NA> <NA> <NA> NA NA <NA> <NA> NA NA
## NA.8 <NA> <NA> <NA> <NA> NA NA <NA> <NA> NA NA
## 60 AC/SG/009 White Male Non-Smoker 166.84 88.25 Vermont Dog 1 43
## NA.9 <NA> <NA> <NA> <NA> NA NA <NA> <NA> NA NA
## NA.10 <NA> <NA> <NA> <NA> NA NA <NA> <NA> NA NA
## 67 AC/SG/064 White Male Non-Smoker 169.16 90.08 Illinois Cat 2 44
## NA.11 <NA> <NA> <NA> <NA> NA NA <NA> <NA> NA NA
## NA.12 <NA> <NA> <NA> <NA> NA NA <NA> <NA> NA NA
## NA.13 <NA> <NA> <NA> <NA> NA NA <NA> <NA> NA NA
## NA.14 <NA> <NA> <NA> <NA> NA NA <NA> <NA> NA NA
## BMI
## NA NA
## 18 31.5
## NA.1 NA
## 23 31.4
## NA.2 NA
## NA.3 NA
## NA.4 NA
## NA.5 NA
## NA.6 NA
## NA.7 NA
## NA.8 NA
## 60 31.7
## NA.9 NA
## NA.10 NA
## 67 31.5
## NA.11 NA
## NA.12 NA
## NA.13 NA
## NA.14 NA
The which function will take a logical vector and return the indices of the TRUE values
## [1] 18 23 60 67
To recap, the set of R commands we have used is:-
patients <- read.delim("data/patient-info.txt")
BMI <- (patients$Weight)/((patients$Height/100)^2)
upper.limit <- mean(BMI,na.rm = TRUE) + 2*sd(BMI,na.rm = TRUE)
plot(BMI)
# Add a horizonal line:
abline(h=upper.limit) read.table() Created and Maintained by Sangram Keshari Sahu
Rmarkdown Template used from Rmdplates package
Licensed under CC-BY 4.0
Source Code At GitHub